package org.naigu.peach.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.naigu.peach.biz.SubjectBiz;
import org.naigu.peach.dal.dao.SubjectDAO;
import org.naigu.peach.dal.dataobject.SubjectDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class SubjectBizImpl implements SubjectBiz{

    private final static Logger logger = LoggerFactory.getLogger(SubjectBizImpl.class);

    @Autowired
    private SubjectDAO subjectDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            SubjectDO subjectDO = subjectDAO.getSubjectById(id);
            bizResult.data.put("subjectDO", subjectDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Subject error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = subjectDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<SubjectDO> subjectList = subjectDAO.selectForPage(baseQuery);
            bizResult.data.put("subjectList", subjectList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Subject list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            subjectDAO.delSubjectById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete subject error", e);
        }
        return bizResult;
    }

    public BizResult create(SubjectDO subjectDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = subjectDAO.insert(subjectDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Subject error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(SubjectDO subjectDO) {
        BizResult bizResult = new BizResult();
        try {
            subjectDAO.updateByIdSelective(subjectDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Subject error", e);
        }
        return bizResult;
    }


}
